"""
图像的裁剪：随机裁剪，中心裁剪
"""
import cv2
import numpy as np

# 随机裁剪
def random_crop(img,w,h):
    # 生成起始的裁剪坐标x,y
    start_x = np.random.randint(0,img.shape[1]-w)
    start_y = np.random.randint(0,img.shape[0]-h)

    new_img = img[start_y:start_y+h+1,start_x:start_x+w+1]

    return new_img

# 中心裁剪
def center_crop(img,w,h):
    # 生成起始的裁剪坐标x,y
    start_x = int(img.shape[1]/2) - int(w/2)
    if start_x < 0:
        start_x = 0
    start_y = int(img.shape[0]/2) - int(h/2)
    if start_y < 0:
        start_y = 0

    new_img = img[start_y:start_y+h+1,start_x:start_x+w+1]
    return new_img

if __name__ == '__main__':
    img = cv2.imread('../data/banana_1.png')
    cv2.imshow('img',img)

    # 随机裁剪
    new_img = random_crop(img,200,200)
    cv2.imshow('random',new_img)

    # 中心裁剪
    new_img1 = center_crop(img,200,200)
    cv2.imshow('center',new_img1)

    cv2.waitKey()
    cv2.destroyAllWindows()